package com.example.express_user.controller;

import com.example.express_user.domain.MyResponse;
import com.example.express_user.domain.User;
import com.example.express_user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("user-controller")
public class UserController {

    @Value("${cbs.filepath}")
    String filePath; // 上传后的路径

    @Autowired
    UserService userService;

    @RequestMapping("common-user-register-courier")
    public MyResponse commonUserRegisterCourier(String realName, String idCard,
                                                String familyAddress, String userSex, String userPhone) {
        return userService.commonUserRegisterCourier(realName, idCard, familyAddress, userSex, userPhone);
    }

    @RequestMapping("update-user-net-name")
    public MyResponse updateUserNetName(String userPhone, String userNetName) {
        return userService.updateUserNetName(userPhone, userNetName);
    }

    @RequestMapping("update-delivery-address")
    public MyResponse updateDeliveryAddress(String userPhone, String deliveryAddress) {
        System.out.println(userPhone);
        System.out.println(deliveryAddress);
        return userService.updateDeliveryAddress(userPhone, deliveryAddress);
    }

    @RequestMapping("update-user-face")
    @Transactional
    public MyResponse updateUserFace(@RequestParam("image_file") MultipartFile imageFile, String userPhone){
        if (imageFile.isEmpty()) {
            System.out.println("文件为空空");
        }
        String fileName = imageFile.getOriginalFilename();  // 文件名
/*        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        fileName = UUID.randomUUID() + suffixName; // 新文件名*/
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            imageFile.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        MyResponse response = userService.updateUserFace(userPhone, fileName);
        return response;
    }
    @RequestMapping("find-user-info-by-phone")
    public User findUserInfoByPhone(String userPhone){
        System.out.println(userService.findUserInfoByPhone(userPhone));
        return userService.findUserInfoByPhone(userPhone);

    }

}